package com.ccit.zhxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.zhxy.pojo.Clazz;
import com.ccit.zhxy.service.ClazzService;
import com.ccit.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 鹏少
 * @Date 2023/12/26
 * @Date 14:08
 */
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @ApiOperation("班级分页查询")
    @GetMapping("/getClazzsByOpr/{pageNo}/{pageSize}")
    public Result getClazzsByOpr(
            @ApiParam("分页查询的页码") @PathVariable Integer pageNo,
            @ApiParam("分页查询的每页大小") @PathVariable Integer pageSize,
            @ApiParam("年级名称") String gradeName,
            @ApiParam("模糊查询的名字") String name) {

        Page<Clazz> clazzPage = new Page<>(pageNo, pageSize);
        Page<Clazz> page = clazzService.lambdaQuery().like(name != null, Clazz::getName, name)
                .eq(gradeName != null, Clazz::getGradeName, gradeName).orderByAsc(Clazz::getId).page(clazzPage);

        return Result.ok(page);

    }


    @ApiOperation("批量删除班级")
    @DeleteMapping("/deleteClazz")
    public Result deleteClazz(
            @ApiParam("批量删除的id") @RequestBody List<Integer> ids) {
        clazzService.removeByIds(ids);
        return Result.ok("删除成功");

    }

    @ApiOperation("根据id修改或者新增班级")
    @PostMapping("/saveOrUpdateClazz")
    public Result saveOrUpdateClazz(
            @ApiParam("JSON转换后端Clazz数据模型") @RequestBody Clazz clazz) {
        clazzService.saveOrUpdate(clazz);
        return Result.ok("操作成功");

    }

    @ApiOperation("查询所有的班级名称")
    @GetMapping("/getClazzs")
    public Result getClazzs(){

        List<Clazz> list = clazzService.list();
        return Result.ok(list);
    }





}
